package com.ssii.www.myproject.query;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

public abstract class BaseQuery {
  @Setter
  @Getter
  private Integer page =1;
  @Setter
  @Getter
  private Integer rows =10;
  @Setter
  @Getter
  private String q;

//  //当前页
  private Integer currentPage = 1;
//  //每页条数
  private Integer pageSize = 10;
  //根据指定字段进行排序
  @Setter
  @Getter
  private String orderByName;
  //默认排序规则为升序
  @Setter
  @Getter
  private String orderByType = "ASC";

  /**
   * 只要你做高级查询，你子类必须去实现该抽象方法
   * @return
   */
  public abstract Specification createSpec();

  /**
   * 排序
   * @return
   */
  public Sort createSort(){
    Sort sort = null;
    if (StringUtils.isNotBlank(orderByName)) {//如果orderByName不为空，证明我们就需要进行排序
      String orderByType = getOrderByType().toUpperCase();
      sort = new Sort("ASC".equals(orderByType) ? Sort.Direction.ASC: Sort.Direction.DESC, orderByName);
    }
    return sort;
  }


  /**
   * 开始页数
   * @return
   */
  public Integer getBeginPage(){
    return page - 1;
  }

  public void setCurrentPage(Integer page){
    this.currentPage = page;
  }

  public Integer getPageSize(){
    return this.rows;
  }
  public void setPageSize(Integer rows) {
    this.pageSize = rows;
  }
}
